package com.zt.dev.item.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.dev.common.ServerResponse;
import com.zt.dev.item.service.SystemItemService;
import com.zt.dev.item.vo.SystemItemVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 30378
 */
@Controller
public class SystemItemController {

    private final SystemItemService service;

    public SystemItemController(SystemItemService service) {
        this.service = service;
    }

    @RequestMapping("/addItem")
    @ResponseBody
    public ServerResponse addItem(SystemItemVo systemItemVo) {
        if (systemItemVo != null && checkSystemZbName(systemItemVo.getZbName())) {
            int a = service.addItem(systemItemVo);
            if (a > 0) {
                return ServerResponse.createBySuccess();
            } else {
                return ServerResponse.createByErrorMessage("添加指标失败");
            }
        } else {
            return ServerResponse.createByErrorMessage("添加指标信息为空");
        }
    }

    private boolean checkSystemZbName(String zbName) {
        return service.checkSystemZbName(zbName);
    }

    @RequestMapping("/delItem")
    @ResponseBody
    public ServerResponse delItem(Integer[] id) {
        if (id.length > 0) {
            service.delItem(id);
            return ServerResponse.createBySuccess("删除成功");
        } else {
            return ServerResponse.createByErrorMessage("前台提交的id为空");
        }
    }

    @RequestMapping("/editItem")
    @ResponseBody
    public ServerResponse editItem(SystemItemVo systemItemVo) {
        if (systemItemVo.getId() != null) {
            if (service.editItem(systemItemVo) > 0) {
                return ServerResponse.createBySuccess("修改成功");
            } else {
                return ServerResponse.createByErrorMessage("修改失败");
            }
        } else {
            return ServerResponse.createByErrorMessage("前台提交的修改对象为空");
        }
    }

    @RequestMapping("/findItem")
    @ResponseBody
    public ServerResponse findItem(Integer id, Page page) {
        if (id == null) {
            return ServerResponse.createBySuccess(service.getAllItems(page));
        } else {
            return ServerResponse.createBySuccess(service.getItemById(id));
        }
    }

}
